//
//  SearchResultCell.swift
//  StoreSearch
//
//  Created by cygarm on 15/12/21.
//  Copyright © 2015年 cygarm. All rights reserved.
//

import UIKit

class SearchResultCell: UITableViewCell {
	@IBOutlet weak var nameLabel: UILabel!
	@IBOutlet weak var artistNameLabel: UILabel!
	@IBOutlet weak var artworkImageView: UIImageView!
	
	var downloadTask: NSURLSessionDownloadTask?
	
	override func awakeFromNib() {
		super.awakeFromNib()
		
		let selectedView = UIView(frame: CGRect.zero)
		selectedView.backgroundColor = UIColor(red: 20/255, green: 160/255, blue: 160/255, alpha: 0.5)
		selectedBackgroundView = selectedView
	}
	
	override func prepareForReuse() {
		super.prepareForReuse()
		
		downloadTask?.cancel()
		downloadTask = nil
		
		nameLabel.text = nil
		artistNameLabel.text = nil
		artworkImageView.image = nil
	}
	
	func configureForSearchResult(searchResult: SearchResult) {
		nameLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
		artistNameLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
		
		nameLabel.text = searchResult.name		
		
		if searchResult.artistName.isEmpty {
			artistNameLabel.text = "Unknown"
		} else {
			artistNameLabel.text = String(format: "%@ (%@)", searchResult.artistName, searchResult.kindForDisplay())
		}
		
		artworkImageView.image = UIImage(named: "Placeholder")
		if let url = NSURL(string: searchResult.artworkURL60) {
			downloadTask = artworkImageView.loadImageWithURL(url)
		}
	}
	
}
